package com.videoaggre.zcl.helper

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.kingja.loadsir.core.LoadService
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.videoaggre.zcl.api.ApiObserver
import com.videoaggre.zcl.helper.loadsir.EmptyCallback
import com.videoaggre.zcl.helper.loadsir.ErrorCallback
import com.videoaggre.zcl.helper.loadsir.LoadSirHelper
import com.videoaggre.zcl.helper.loadsir.LoadingCallback
import com.videoaggre.zcl.ui.adapter.BaseRecyclerAdapter

class RefreshHelper<T, L : List<T>>(
    val refreshLayout: RefreshLayout? = null,
    val recyclerView: RecyclerView,
    val mBaseRecyclerAdapter: BaseRecyclerAdapter<*, T>,
    val isMultipleItem: Boolean = false,
    var mLoadService: LoadService<*>? = null,
    var listener: (() -> Unit)? = null
) {
    var firstPage = 1
        set(value) {
            page = value
            field = value
        }
    var page = firstPage
    private var isOnRefresh = true

    init {
        if (mLoadService == null) {
            mLoadService = LoadSirHelper.register(refreshLayout ?: recyclerView, listener = { onRefresh() })
        }
        recyclerView.adapter = mBaseRecyclerAdapter
        recyclerView.layoutManager = LinearLayoutManager(recyclerView.context)

        refreshLayout?.setOnRefreshListener { onRefresh() }
        refreshLayout?.setOnLoadMoreListener { onLoadMore() }
        //mLoadService?.showCallback(LoadingCallback::class.java)
    }

    fun onRefresh() {
        isOnRefresh = true
        page = firstPage
        listener?.invoke()
    }

    fun onLoadMore() {
        isOnRefresh = false
        page++
        listener?.invoke()
    }

    fun getLastBean(): T? = mBaseRecyclerAdapter.datas.let { if (it.isNotEmpty() && !isOnRefresh) it[it.size - 1] else null }

    fun handApiObserver(error: (() -> Unit)? = null): ApiObserver<L?> {
        return object : ApiObserver<L?>(refreshLayout = refreshLayout, loadService = mLoadService) {
            override fun start() {
                if (mBaseRecyclerAdapter.itemCount == 0) {// 没有数据显示加载视图
                    mLoadService?.showCallback(LoadingCallback::class.java)
                }
            }

            override fun success(t: L?) {
                super.success(t)
                doSuccess(t)
            }

            override fun doError(e: Throwable?): Boolean {
                if (error != null) {
                    error.invoke()
                } else {
                    if (mBaseRecyclerAdapter.itemCount == 0) {// 没有数据显示错误视图
                        mLoadService?.showCallback(ErrorCallback::class.java)
                    }
                }
                return true
            }
        }
    }

    fun doSuccess(t: L?) {
        if (isOnRefresh)
            mBaseRecyclerAdapter.clear()
        if (isMultipleItem) {// 多条目要重置不然多条目会失效
            recyclerView.adapter = mBaseRecyclerAdapter
        }
        if (isOnRefresh && (t == null || t.isEmpty())) {
            mLoadService?.showCallback(EmptyCallback::class.java)
        } else {
            mLoadService?.showSuccess()
        }
        if ((t == null || t.isEmpty()) && page > firstPage) {
            page--
        }
        if (refreshLayout != null && (t == null || t.isEmpty())) {
            refreshLayout.setNoMoreData(true)
        } else refreshLayout?.setNoMoreData(false)
        if ((t != null && t.isNotEmpty())) {
            mBaseRecyclerAdapter.addAll(t)
        }
    }
}